package ywh.manager
{
	import ywh.event.SyncEvent;
	import ywh.stream.MainStream;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 单例类:同步功能类;
	 * @author 杨伟华
	 * @version 1.0
	 *
	 * Created 1/21/2013 10:32 AM
	 */
	public class Sync
	{
		{
			_timer = new Timer(100, 0);
			_timer.addEventListener(TimerEvent.TIMER, onTimerSync);
		}
		static private var _timer:Timer;
		static private var _sync:Boolean;
		
		public function Sync(step:int)
		{
			throw(new Error("Sync类为静态类"));
		}
		
		static public function pause():void
		{
			MainStream.$.pause();
		}
		
		static public function play():void
		{
			MainStream.$.play();
			_timer.start();
		}
		
		static public function get time():Number
		{
			return MainStream.$.time;
		}
		
		static public function set time(value:Number):void
		{
			MainStream.$.time = value;
		}
		
		static public function get duration():Number
		{
			return MainStream.$.duration;
		}
		
		/**
		 * 阻止一次同步事件的发生;
		 */
		static public function preventSyncOnce():void
		{
			_sync = false;
		}
		
		static private function onTimerSync(timerEvent:TimerEvent):void
		{
			_sync = true;
			/**
			 * 欲同步指令；侦听者应该是流对象
			 * 1.流应该指挥播放器更新当前状态;
			 * 2.流可以综合各种信息，如播放器状态，阻止本次同步;
			 * 3.主流应该趁机获取主流时间,用以在后续更新状态;
			 */
			dispathEvent(SyncEvent.PRE_SYNC);
			if (_sync)
			{
				/**
				 * 同步列表，如果列表更新项目;
				 */
				dispathEvent(SyncEvent.SYNC_LIST);
				/**
				 * 同步播放器，更新播放器当前时间;
				 */
				dispathEvent(SyncEvent.SYNC_PLAYER);
			}
		}
		
		static private function dispathEvent(SyncEventType:String):void
		{
			Core.dispatchEvent(new SyncEvent(SyncEventType, time));
		}
	}
}